//
//  VideoPlayerViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/24.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "VideoPlayerViewController.h"

#import "VideoPlayerTableViewCell.h"

#import "CustomerNetworking.h"

#import <MediaPlayer/MediaPlayer.h>

#import <MBProgressHUD.h>

#import "LGCLastViewController.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)


@interface VideoPlayerViewController ()<UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate>

//@property (nonatomic, retain) UIView *totalView;
@property (nonatomic, copy) NSString *totalUrl;
@property (nonatomic, retain) NSMutableArray *playArr;

@property (nonatomic, retain) UIView *headView;

@property (nonatomic, retain) UILabel *titleName;

@property (nonatomic, retain) UIImageView *tagImageView;

@property (nonatomic, retain) UILabel *tagLabel;

@property (nonatomic, retain) UIImageView *likeImageView;

@property (nonatomic, retain) UILabel *likeCountLabel;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray * tuiMVArr;

@property (nonatomic, retain) MBProgressHUD *HUD;

/**
 *  播放器
 */
@property (nonatomic, retain)MPMoviePlayerController *mpMoviePlayer;


@end

@implementation VideoPlayerViewController



- (void)viewWillAppear:(BOOL)animated
{
    LGCLastViewController * detail = [LGCLastViewController shareLastViewController];
    [detail.player stop];
}



- (void)dealloc
{
//    [_totalView release];
    [_headView release];
    [_titleName release];
    [_tagImageView release];
    [_tagLabel release];
    [_likeImageView release];
    [_likeCountLabel release];
    [_tableView release];
    [_tuiMVArr release];
    [super dealloc];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tuiMVArr = [NSMutableArray array];
    self.playArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //去掉下划线
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    
    [self createSubViews];
    [self loadNums];
    [self loadWebs];
}
-(void)returnPage:(UIButton *)sender{
    [self.mpMoviePlayer stop];
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)loadWebs
{
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    //显示的文字
    self.HUD.labelText = @"正在进行加载~";
    
    NSString *frontUrl =[NSString stringWithFormat:@"http://api.dongting.com/song/video?videoids=%@", _vid]; ;
    NSString *gidUrl = @"&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679607422799&longtitude=121.5285247867256";
    self.totalUrl = [NSString stringWithFormat:@"%@%@", frontUrl, gidUrl];
    
    [CustomerNetworking customerWithURL:self.totalUrl andParamer:nil andBlock:^(id response) {
        for (NSDictionary * dic in [response objectForKey:@"data"]) {
            NSLog(@"🍎%@", dic);

            self.titleName.text = [dic objectForKey:@"videoName"];
            self.tagLabel.text = [NSString stringWithFormat:@"◀️%@", [dic objectForKey:@"pickCount"]];
            self.likeCountLabel.text = [NSString stringWithFormat:@"💗%@", [dic objectForKey:@"bulletCount"]];
        }
    }];
    
    [CustomerNetworking customerWithURL:self.totalUrl andParamer:nil andBlock:^(id response) {
        for (NSDictionary * dic in [[[response objectForKey:@"data"] objectAtIndex:0] objectForKey:@"mvList"] ) {
            
            

           NSString * str = [dic objectForKey:@"url"];
            [self.playArr addObject:str];
            
            
            //播放器地址赋值
            NSURL *url = [NSURL URLWithString: [self.playArr objectAtIndex:0]];
            [self.mpMoviePlayer setContentURL:url];
            [self.mpMoviePlayer play];
            
        }
       [self.HUD hide:YES];
    }];
    

}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self.mpMoviePlayer stop];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [self.mpMoviePlayer play];
}
- (void)loadNums
{
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://api.dongting.com/sim/mv/%@/similarity?app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=9.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.88251801858907&longtitude=121.5396789787246", _vid] andParamer:nil andBlock:^(id response) {
        for (NSDictionary * dic in [response objectForKey:@"data"]) {
            MVClassModel * model = [MVClassModel modelWithDictionary:dic];
            [self.tuiMVArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}


-(void)createSubViews
{
        //播放器
    self.mpMoviePlayer = [[MPMoviePlayerController alloc]init];
    self.mpMoviePlayer.view.frame = CGRectMake(0, HEIGHT_PRO(20), self.view.frame.size.width, SCREEN_HEIGHT / 3);
    self.mpMoviePlayer.shouldAutoplay = YES;
    [self.mpMoviePlayer play];
    [self.view addSubview:self.mpMoviePlayer.view];
    
    //返回
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    btnBack.frame = CGRectMake(WIDTH_PRO(20), HEIGHT_PRO(20), WIDTH_PRO(50), HEIGHT_PRO(40));
    [btnBack setTitle:@"返回" forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btnBack.backgroundColor = [UIColor whiteColor];
    [btnBack addTarget:self action:@selector(returnPage:) forControlEvents:UIControlEventTouchUpInside];
    [self.mpMoviePlayer.view addSubview:btnBack];
    

    
    self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, self.mpMoviePlayer.view.frame.size.height + HEIGHT_PRO(20), SCREEN_WIDTH, HEIGHT_PRO(70))];
   // [self.view addSubview:self.headView];
    [_headView release];
    
    self.titleName = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(20), HEIGHT_PRO(10), SCREEN_WIDTH - WIDTH_PRO(40), HEIGHT_PRO(20))];
    [self.headView addSubview:self.titleName];
    self.titleName.font = [UIFont systemFontOfSize:15];
    [_titleName release];
    
    self.tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(20), self.titleName.frame.origin.y + self.titleName.frame.size.height + HEIGHT_PRO(10), self.titleName.frame.size.width / 3, HEIGHT_PRO(20))];
    self.tagLabel.textColor = [UIColor grayColor];
    self.tagLabel.font = [UIFont systemFontOfSize:12];
    [self.headView addSubview:self.tagLabel];
    [_tagLabel release];
    
    self.likeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(20) + self.titleName.frame.size.width / 3, self.titleName.frame.origin.y + self.titleName.frame.size.height + HEIGHT_PRO(10), self.titleName.frame.size.width / 3, HEIGHT_PRO(20))];
    self.likeCountLabel.textColor = [UIColor grayColor];
    self.likeCountLabel.font = [UIFont systemFontOfSize:12];
    [self.headView addSubview:self.likeCountLabel];
    [_likeCountLabel release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.mpMoviePlayer.view.frame.size.height + HEIGHT_PRO(20), SCREEN_WIDTH, SCREEN_HEIGHT - self.mpMoviePlayer.view.frame.size.height - HEIGHT_PRO(20)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview :self.tableView];
    [_tableView release];
    self.tableView.tableHeaderView = self.headView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tuiMVArr.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"推荐MV";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoPlayerTableViewCell * cell = [VideoPlayerTableViewCell VideoPlayerTableViewCellWithTableView:tableView];
    MVClassModel * model = [self.tuiMVArr objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     MVClassModel * model = [self.tuiMVArr objectAtIndex:indexPath.row];
    NSLog(@"xiao%@" ,model);
    ListModel *listModel = model.mvList[0];
    NSLog(@"🍎%@", listModel);
    
    [self.mpMoviePlayer stop];
    [self.mpMoviePlayer setContentURL:[NSURL URLWithString:listModel.url]];
    [self.mpMoviePlayer play];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
